<script setup>
import {reactive, ref} from "vue";
import {useRouter} from "vue-router";
import {loginAPI} from "@/apis/user.js";
import { useUserStore} from "@/stores/userStore.js";
const userStore = useUserStore()

const loginForm = reactive({
  account:'heima293',
  password:'hm#qd@23!',
  agree:true
})

const rules = {
  account: [
    { required: true, message:'用户名不能为空', trigger: 'blur'}
  ],
  password: [
    { required: true, message:'密码不能为空', trigger:'blur'},
    { min:6, max: 14, message: '密码长度为6-14个字符', trigger: 'blur'}
  ],
  agree: [
    {
      validator: (rule, value, callback) => {
        if (value) {
          callback()
        } else {
          callback(new Error('请勾选协议!'))
        }
      }
    }
  ]
}

const loginFormRef = ref(null)
const router = useRouter()

const checkLogin = () => {
  loginFormRef.value.validate(async (valid) => {
    const { account, password } = loginForm
    console.log(valid)

    if (valid) {
      await userStore.getUserInfo({account,password})
      ElMessage({
        type:'success',
        message:'登陆成功'
      })
      router.replace('/')
    }
  })
}
</script>
<template>
  <div>
    <header class="login-header">
      <div class="container">
        <h1 class="logo">
          <RouterLink to="/">首页</RouterLink>
        </h1>
        <RouterLink to="/">
          进入首页 >>
        </RouterLink>
      </div>
    </header>
    <section class="login-section">
        <div class="wrapper">
          <nav>
            <a href="">账户登陆</a>
          </nav>
          <div class="account-box">
            <div class="form" @keyup.enter="checkLogin">
              <el-form label-position="right" label-width="60px" :model="loginForm" :rules="rules" ref="loginFormRef">
                <el-form-item label="账户" prop="account">
                  <el-input type="text" v-model="loginForm.account"/>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                  <el-input type="password" v-model="loginForm.password"/>
                </el-form-item>
                <el-form-item prop="agree">
                  <el-checkbox size="large" v-model="loginForm.agree">
                    我已阅读隐私协议和服务条款
                  </el-checkbox>
                </el-form-item>
                <el-button size="large" class="subBtn" @click="checkLogin">点击登陆</el-button>
              </el-form>
            </div>
          </div>
        </div>
    </section>

    <footer class="login-footer">
      <div class="container">
        <p>
          <a href="">关于我们</a>
          <a href="">帮助中心</a>
          <a href="">配送与验收</a>
          <a href="">售后服务</a>
          <a href="">搜索推荐</a>
          <a href="">友情链接</a>
          <a href="">商务合作</a>
        </p>
        <p>CopyRight &copy; </p>
      </div>
    </footer>
  </div>
</template>
<style scoped lang="scss">
.login-header {
  background-color: #fff;
  border-bottom: 1px solid #e4e4e4;

  .container {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .logo {
    width: 200px;

    a {
      display: block;
      height: 132px;
      width: 100%;
      text-indent: -9999px;
      background: url("@/assets/images/jg-logo.png") no-repeat  19px / contain;
      background-position: center center;
    }
  }

  .sub {
    flex: 1;
    font-size: 24px;
    font-weight: normal;
    margin-bottom: 38px;
    margin-left: 20px;
    color: #666;
  }

  .entry {
    width: 12px;
    margin-bottom: 38px;
    font-size: 16px;

    i {
      font-size: 14px;
      color: $xtxColor;
      letter-spacing: -5px;

    }
  }
}

.login-section {
  height: 488px;
  position: relative;
  background: #e93854 url("@/assets/images/login-bg-jg.png") no-repeat center center;

  .wrapper {
    width: 380px;
    background: #fff;
    position: absolute;
    left: 50%;
    top: 54px;
    transform: translate3d(100px,0,0);
    box-shadow: 0 0 10px rgba(0,0,0,0.15);

    nav {
      font-size: 14px;
      height: 55px;
      margin-bottom: 20px;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      padding: 0 40px;
      text-align: right;
      align-items: center;

      a {
        flex:1;
        line-height: 1;
        display: inline-block;
        font-size: 18px;
        position: relative;
        text-align: center;
      }
    }
  }
}

.login-footer {
  padding: 30px 0 50px;
  background: #fff;

  p {
    text-align: center;
    color: #999;
    padding-top: 20px;

    a {
      line-height: 1;
      padding: 0 10px;
      color: #999;
      display: inline-block;

      ~a {
        border-left: 1px solid #ccc;
      }
    }
  }
}

.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;

    a {
      color: $xtxColor;
    }

    i {
      font-size: 14px;
    }
  }
}

.form {
  padding: 0 20px 20px 20px;

  &-item {
    margin-bottom: 28px;

    .input {
      position: relative;
      height: 36px;

      > i {
        width: 34px;
        height: 34px;
        background: #cfcdcd;
        color: #fff;
        position: absolute;
        left: 1px;
        top: 1px;
        text-align: center;
        line-height: 34px;
        font-size: 18px;
      }

      input {
        padding-left: 44px;
        border: 1px solid #cfcdcd;
        height: 36px;
        line-height: 36px;
        width: 100%;

        &.error {
          border-color: $priceColor;
        }

        &.active,
        &.focus {
          border-color: $xtxColor;
        }
      }

      .code {
        position: absolute;
        right: 1px;
        top: 1px;
        text-align: center;
        line-height: 34px;
        font-size: 14px;
        background: #f5f5f5;
        color: #666;
        height: 34px;
        cursor: pointer;
      }
    }
    >.error {
      position: absolute;
      font-size: 12px;
      line-height: 28px;
      color: $xtxColor;

      i {
        font-size: 14px;
        margin-right: 2px;
      }
    }
  }
  .agree {
    a {
      color: #069;
    }
  }
  .btn {
    display: flex;
    width: 100%;
    height: 20px;
    color: #fff;
    text-align: center;
    line-height: 40px;
    background: $xtxColor;

    &.disabled {
      background: #cfcdcd;
    }
  }
}

.action {
  padding: 20px 40px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .url {
    a {
      color: #999;
      margin-left: 10px;
    }
  }
}

.subBtn {
  background: $xtxColor;
  width: 100%;
  color: #fff;
}
</style>